package com.gp.pj.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gp.pj.entity.Employee;
import com.gp.pj.entity.Role;
import com.gp.pj.entity.User;
import com.gp.pj.entity.UserRole;
import com.gp.pj.service.EmployeeService;
import com.gp.pj.service.RoleService;
import com.gp.pj.service.UserRoleService;
import com.gp.pj.service.UserService;
import com.gp.pj.utils.MD5Utils;
import com.gp.pj.utils.ResultUtils;
import com.gp.pj.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Chloe
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private EmployeeService employeeService;

    @Resource
    private RoleService roleService;

    @Resource
    private UserRoleService userRoleService;

    @ResponseBody
    @GetMapping(value = "/get_user_list")
    public ResultUtils userList() {
        JSONArray data = userService.getUserList();
        return ResultUtils.success(data);
    }

    @ResponseBody
    @PostMapping(value = "/saveUser")
    public ResultUtils saveUser(@RequestBody JSONObject jsonRe) {
        UserVo userVo = jsonRe.toJavaObject(UserVo.class);
        //获取empName保存到emp表中
        Employee employee = new Employee();
        employee.setName(userVo.getEmpName());
        employee.setType(userVo.getType());
        employeeService.save(employee);
        //获取emp表的主键id
        User user = new User();
        user.setEmpId(Long.valueOf(employee.getId()));
        //将密码进行加密
        String password = userVo.getPassword();
        try{
            password = MD5Utils.encodePwd(password);
            user.setPassword(password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //保存到user表
        user.setStatus(userVo.getStatus());
        user.setName(userVo.getUserName());
        userService.save(user);
        JSONArray access = userVo.getAccess();
        UserRole userRole = new UserRole();
        //查询角色id,保存到t_user_role表中
        access.forEach(item ->{
            List<Role> roleList = roleService.lambdaQuery().eq(Role::getName,item).list();
            userRole.setRoleId(roleList.get(0).getId());
            userRole.setUserId(Long.valueOf(user.getId()));
            userRoleService.save(userRole);
        });
        return ResultUtils.success();
    }

    @ResponseBody
    @PostMapping(value = "/updateUser")
    public ResultUtils updateUser(@RequestBody JSONObject jsonRe) {
        UserVo userVo = jsonRe.toJavaObject(UserVo.class);
        //设置密码为null确保不更新密码
        User user = new User();
        user.setPassword(null);
        user.setId(Long.valueOf(userVo.getUserId()));
        user.setName(userVo.getUserName());
        user.setStatus(userVo.getStatus());
        userService.updateById(user);
        //将user中对应emp的自段赋值,并更新emp表
        Employee employee = new Employee();
        employee.setId(Long.valueOf(userVo.getEmpId()));
        employee.setName(userVo.getEmpName());
        employee.setType(userVo.getType());
        employeeService.updateById(employee);
        //将原有user和role的关联数据删除
        JSONArray access = userVo.getAccess();
        UserRole userRole = new UserRole();
        Map<String, Object> fitter = new HashMap<>();
        fitter.put("user_id",userVo.getUserId());
        userRoleService.removeByMap(fitter);
        //重新保存user和role的关联数据
        access.forEach(item ->{
            List<Role> roleList = roleService.lambdaQuery().eq(Role::getName,item).list();
            userRole.setRoleId(roleList.get(0).getId());
            userRole.setUserId(Long.valueOf(userVo.getUserId()));
            userRoleService.save(userRole);
        });
        return ResultUtils.success();
    }

    @ResponseBody
    @PostMapping(value = "/deleteUser")
    public ResultUtils deleteUser(@RequestBody JSONObject jsonRe) {
        UserVo userVo = jsonRe.toJavaObject(UserVo.class);
        userService.removeById(userVo.getUserId());
        employeeService.removeById(userVo.getEmpId());
        Map<String, Object> fitter = new HashMap<>();
        fitter.put("user_id",userVo.getUserId());
        userRoleService.removeByMap(fitter);
        return ResultUtils.success();
    }

    @ResponseBody
    @PostMapping(value = "/updateUserStatus")
    public ResultUtils updateUserStatus(@RequestBody JSONObject jsonRe) {
        UserVo userVo = jsonRe.toJavaObject(UserVo.class);
        User userStauts = new User();
        String status = userVo.getStatus();
        userStauts.setId(Long.valueOf(userVo.getUserId()));
        if ( "enable".equals(status)){
            userStauts.setStatus("disabale");
        }else {
            userStauts.setStatus("enable");
        }
        userService.updateById(userStauts);
        return ResultUtils.success();
    }

}
